﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BestLifePOS_Core.Data.Commands;
using BestLifePOS_Core.DomainObjects;
using BestLifePOS_Core.Data.Fascade;
using System.Linq;

namespace BestLifePOS_PresentationLayer
{
    public partial class frmNewPO : Form
    {
        DataTable LvTable = new DataTable("Details");
        DataTable dtChecked = new DataTable("Details");
        List<string> LvList2 = new List<string>();
        public frmNewPO()
        {
            InitializeComponent();
            DataGridViewCheckBoxColumn check = new DataGridViewCheckBoxColumn();
            check.HeaderText = "Check Item";
            check.FalseValue = "0";
            check.TrueValue = "1";
            check.Width = 20;
            dgvApprovedPR.Columns.Insert(0, check);
        }

        private void frmNewPO_Load(object sender, EventArgs e)
        {
            FillSupplierDropDown();
            ApprovedPRFillGrid();
            FillGrid3();
        }

        private void ApprovedPRFillGrid()
        {

            //Fill the DataGrid
            Purchase_Request LvPR = new Purchase_Request();
            //LvPR.status = "APPROVED";
            List<Purchase_Request> LvList = Purchase_RequestDAO.GetAllApprovedPR();

            DataTable LvTable = new DataTable();
            dgvApprovedPR.DataSource = LvTable;

            LvTable.Columns.Add("PRID ");
            LvTable.Columns.Add("BranchID ");
            LvTable.Columns.Add("DateCreated ");
            LvTable.Columns.Add("Description ");
            LvTable.Columns.Add("TotalVal ");

            foreach (Purchase_Request LvPR2 in LvList)
            {
                LvTable = (DataTable)dgvApprovedPR.DataSource;

                LvTable.Rows.Add(LvTable.NewRow());

                LvTable.Rows[LvTable.Rows.Count - 1][0] = LvPR2.Pr_Id.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][1] = LvPR2.Branch_Id.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][2] = LvPR2.Create_Date.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][3] = LvPR2.Description.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][4] = LvPR2.Total.ToString();


            }
        }

        private void FillGrid3()
        {

            dgvPOItems.DataSource = LvTable;

            LvTable.Columns.Add("PRID ");
            LvTable.Columns.Add("Qty ");
            LvTable.Columns.Add("PLID ");

        }



        private void PRDetailsFillGrid(string PPRID)
        {

            //Fill the DataGrid
            Purchase_Request LvPR = new Purchase_Request();
            //LvPR.status = "APPROVED";
            List<Purchase_Request> LvList = Purchase_RequestDAO.GetAllPRbyPRID(PPRID);

            foreach (Purchase_Request LvPR2 in LvList)
            {



                if ((dgvPOItems.RowCount) == 0)
                {


                    LvTable = (DataTable)dgvPOItems.DataSource;

                    LvTable.Rows.Add(LvTable.NewRow());

                    LvTable.Rows[LvTable.Rows.Count - 1][0] = LvPR2.Pr_Id.ToString();
                    LvTable.Rows[LvTable.Rows.Count - 1][1] = LvPR2.Qty.ToString();
                    LvTable.Rows[LvTable.Rows.Count - 1][2] = LvPR2.PL_Id.ToString();
                }

                else
                {
                    //testing
                    String searchValue = LvPR2.PL_Id.TrimEnd().ToString(); ;
                    int rowIndex = -1;
                    foreach (DataGridViewRow row in dgvPOItems.Rows)
                    {
                        if (row.Cells[2].Value != null)
                        {
                            if (row.Cells[2].Value.ToString().TrimEnd().Equals(searchValue))
                            {
                                rowIndex = row.Index;
                                break;
                            }
                        }
                    }
                    if (rowIndex == -1)
                    {
                        LvTable.Rows.Add(LvTable.NewRow());

                        LvTable.Rows[LvTable.Rows.Count - 1][0] = LvPR2.Pr_Id.ToString();
                        LvTable.Rows[LvTable.Rows.Count - 1][1] = LvPR2.Qty.ToString();
                        LvTable.Rows[LvTable.Rows.Count - 1][2] = LvPR2.PL_Id.ToString();

                    }
                    else
                    {
                        LvTable.Rows[rowIndex][1] = (decimal.Parse(LvTable.Rows[rowIndex][1].ToString()) + decimal.Parse(LvPR2.Qty.ToString())).ToString();

                    }


                }



            }
        }


        private void dgvApprovedPR_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            DataGridView dgv = (DataGridView)sender;
            DataGridViewCell cell = dgv.CurrentCell;
            if (cell.RowIndex >= 0 && cell.ColumnIndex == 3) // My checkbox column
            {
                // If checkbox checked, copy value from col 1 to col 2
                if (dgv.Rows[cell.RowIndex].Cells[cell.ColumnIndex].EditedFormattedValue != null && dgv.Rows[cell.RowIndex].Cells[cell.ColumnIndex].EditedFormattedValue.Equals(true))
                {
                    dgv.Rows[cell.RowIndex].Cells[1].Value = dgv.Rows[cell.RowIndex].Cells[2].Value;
                   
                }
            }
        }

        private void dgvApprovedPR_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {


             DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();

             ch1 = (DataGridViewCheckBoxCell)dgvApprovedPR.Rows[dgvApprovedPR.CurrentRow.Index].Cells[0];

             string Lvtest = dgvApprovedPR.Rows[e.RowIndex].Cells[1].Value.ToString();
             txtBranchId.Text = dgvApprovedPR.Rows[e.RowIndex].Cells[2].Value.ToString();

            if (ch1.Value == null)

                ch1.Value=false;

             switch (ch1.Value.ToString())

             {

                 case "True":
                    // PRDetailsFillGrid(Lvtest);
                     //ch1.Value = false;

                     break;

                 case "False":
                     PRDetailsFillGrid(Lvtest);
                     LvList2.Add(Lvtest);
                     
                     //ch1.Value = true;

                     break;

             }
            
           // MessageBox.Show(ch1.Value.ToString());



        }

        private void dgvApprovedPR_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            //if (Convert.ToInt32(dgvApprovedPR.Rows[e.RowIndex].Cells[0].Value.ToString()) == 1 && dgvApprovedPR.CurrentCell is DataGridViewCheckBoxCell)
            //{
            //    string Lvtest = dgvApprovedPR.Rows[e.RowIndex].Cells[1].Value.ToString();

            //}
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            txtPoId.Text = Purchase_OrderDAO.GetNewPOId().ToString();
        }

        //private void button47_Click(object sender, EventArgs e)
        //{

        //}

        private void FillSupplierDropDown()
        {
            //Fill the DataGrid
            List<Supplier> LvList = SupplierDAO.GetAllSupplier();
            DataTable LvTable = new DataTable();

            //cmbCategory.DataSource = LvTable;
            LvTable.Columns.Add("SupplierID");
            LvTable.Columns.Add("SupplierName");

            string lvtest = "";

            foreach (Supplier LvSupplier in LvList)
            {

                LvTable.Rows.Add(LvTable.NewRow());

                lvtest = LvSupplier.Sup_ID.ToString() + '-' + LvSupplier.Sup_Name.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][0] = lvtest;

            }

            cmbSupplier.DisplayMember = "SupplierID";
            cmbSupplier.ValueMember = "SupplierID";
            cmbSupplier.DataSource = LvTable;

        }


        ///Saving the PO Details

        private int SavePO(string xmlstring)
        {
            string Lvxml = xmlstring;

            Purchase_Order LvPO = CollectPOFigures();
            if (LvPO != null)
            {
                return Purchase_OrderDAO.SavePO(LvPO, Lvxml, LvList2);
            }
            else
            {
                return 0;
            }
        }

        private Purchase_Order CollectPOFigures()
        {

            try
            {

                Purchase_Order LvPOMaster = new Purchase_Order();

                LvPOMaster.Po_Id = txtPoId.Text.ToString();
                LvPOMaster.Sup_Id = cmbSupplier.Text.ToString().Substring(0, cmbSupplier.Text.IndexOf('-'));
                LvPOMaster.Branch_Id = txtBranchId.Text.ToString();  //txtBranchId.Text.ToString().Substring(0, txtBranchId.Text.IndexOf('-'));
                LvPOMaster.Description = rtxtDescription.Text.ToString();
                LvPOMaster.User_Id = "1";
                LvPOMaster.Po_Date = DateTime.Now;


                return LvPOMaster;
            }
            catch (Exception)
            {
                return null;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            DataSet ds = new DataSet("podetails");

            string detailsXML = "";

            ds.Tables.Add(LvTable.Copy());
            detailsXML = ds.GetXml();

            SavePO(detailsXML);
            MessageBox.Show("PO Was Succesfuly Saved");
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            frmReportViewer frmR = new frmReportViewer(2, txtPoId.Text.ToString());
            frmR.ShowDialog();
        }


        
    }
}
